class EventBus {
  constructor() {
    this.events = this.events || [];
  }

  /* eslint-disable no-plusplus */
  emit = (type, ...args) => {
    const e = this.events[type];
    if (Array.isArray(e)) {
      for (let i = 0; i < e.length; i++) {
        e[i].apply(this, args);
      }
    } else if (e && e[0]) {
      e[0].apply(this, args);
    }
  };

  // if fun need this, bind(this) before addListener
  // 如果需要指回this，需要在传入fun前，通过bind绑定this
  on(type, fun) {
    const e = this.events[type];
    if (!e) {
      this.events[type] = [fun];
    } else {
      e.push(fun);
    }
  }

  // can't be anonymous function
  // 不能使用匿名函数，否则无法关闭监听

  off(type, fun) {
    const e = this.events[type];
    if (e) {
      for (let i = 0; i < e.length; i++) {
        if (e[i] === fun) {
          e.splice(i, 1);
          break;
        }
      }
    }
  }
}

const eventBus = new EventBus();
export default eventBus;
